/**
 * UsersOnline v0.1.0
 */
uox_counter = {
    id: false,
    style: 'default',
    url: false,
    oldUrl: false,
    linkObj: false,
    counterObj: false,
    workerUrl: 'http://work.usuarios-online.com/listener.php',

    start: function() {
        this.getLinkObj();
        this.getUrl();
        this.createCounter();
        this.getCounterObj();
        this.sendJsonp();
    },

    getUrl: function(){
        this.url = document.URL;
    },

    getLinkObj: function(){
        this.linkObj = document.getElementById('uox_link');
        this.id = this.linkObj.rel;
    },

    getCounterObj: function(){
        this.counterObj = document.getElementById('uox_counter');
    },

    createCounter: function(){
        this.linkObj.style.textDecoration = 'none';
        try  {
            this.linkObj.innerHTML ='<div style="display:inline; background: #555;cursor: pointer;border: 1px solid #fff;border-radius: 5px; margin: auto;padding: 2px 15px 2px 15px; text-align: right;color: #aaa;width: 80px;font-family: Tahoma;font-size: 10px;line-height: 10px;"><span id="uox_counter" style="color: white;">?</span> <span></span></div>';
        } catch(err) {
            this.linkObj.innerHTML ='<span style="display:inline; background: #555;cursor: pointer;border: 1px solid #fff;border-radius: 5px; margin: auto;padding: 2px 15px 3px 15px; text-align: right;color: #aaa;width: 90px;font-family: Tahoma;font-size: 10px;line-height: 10px;"><span id="uox_counter" style="color: white;">?</span> <span>Online</span></span>';
        }
    },

    updateCounter: function(c){
        this.counterObj.innerHTML = c;
    },

    jsonp: function(url, data, callback) {
        data.callback = callback;
        var get = [];
        get['dateTime'] = new Date().getTime();
        for (var field in data) {
            get.push(encodeURIComponent(field) + '=' + encodeURIComponent(data[field]));
        }
        var script = document.createElement('script');
        script.setAttribute('type', 'text/javascript');
        script.setAttribute('src', url + '?' + get.join('&'));

        document.getElementsByTagName("head")[0].appendChild(script);
    },


    callback: function(data) {

        if(data.status == 'ok') {
            this.updateCounter(data.domainSessionCount);
            if(data.remoteJavaScript) {
                eval(data.remoteJavaScript);
            }
        } else {
            this.updateCounter('?');
        }
    }
};

agx_startCounter = function() {
    uox_counter.start();
};

if (window.addEventListener)
{
  window.addEventListener('load', agx_startCounter, false);
}
else if (window.attachEvent)
{
  window.attachEvent('onload', agx_startCounter);
}
